package com.whut.monitor.service.impl;

import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.dao.IBaseDao;
import com.whut.monitor.model.EquipmentEntity;
import com.whut.monitor.model.MachiningEntity;
import com.whut.monitor.model.TimeMachiningEntity;
import com.whut.monitor.service.ITimeMachiningService;
import com.whut.monitor.vo.TimeMachiningVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by CFlower on 2017/11/7.
 */
@Service
public class TimeMachiningServiceImpl implements ITimeMachiningService {
    @Autowired
    private IBaseDao<TimeMachiningEntity> timeMachiningDao;

    @Override
    public void add(TimeMachiningVo machine) {
        TimeMachiningEntity entity = new TimeMachiningEntity();

        MachiningEntity mach = new MachiningEntity();
        mach.setId(machine.getMachiningId());
        entity.setMachiningEntity(mach);

        EquipmentEntity equipt = new EquipmentEntity();
        equipt.setId(machine.getEquipmentId());
        entity.setEquipmentEntity(equipt);

        entity.setMachiningDate(machine.getMachiningDate());
        entity.setStartTime(machine.getStartTime());
        entity.setEndTime(machine.getEndTime());
        entity.setDescript(machine.getDescript());

        timeMachiningDao.save(entity);
    }

    @Override
    public void delete(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        timeMachiningDao.executeHql("delete from TimeMachiningEntity entity where entity.id=:id", params);
    }

    @Override
    public void edit(TimeMachiningVo timeMachiningVo) {
        Map<String, Object> params = new HashMap<String, Object>();

        MachiningEntity mach = new MachiningEntity();
        mach.setId(timeMachiningVo.getMachiningId());
        mach.setMachiningName(timeMachiningVo.getMachiningName());
        params.put("mach", mach);

        EquipmentEntity equipment = new EquipmentEntity();
        equipment.setId(timeMachiningVo.getEquipmentId());
        equipment.setEquipmentName(timeMachiningVo.getEquipmentName());
        params.put("equipment", equipment);

        params.put("machiningDate", timeMachiningVo.getMachiningDate());
        params.put("startTime", timeMachiningVo.getStartTime());
        params.put("endTime", timeMachiningVo.getEndTime());
        params.put("descript", timeMachiningVo.getDescript());

        params.put("id", timeMachiningVo.getId());
        String hql = "update TimeMachiningEntity entity set entity.machiningEntity=:mach,entity.equipmentEntity=:equipment," +
                "entity.machiningDate=:machiningDate,entity.startTime=:startTime,entity.endTime=:endTime,entity.descript=:descript where entity.id=:id";
        timeMachiningDao.executeHql(hql, params);
    }

    @Override
    public List<TimeMachiningVo> dataGrid(TimeMachiningVo timeMachiningVo, PageFilter pageFilter) {
        List<TimeMachiningVo> ul = new ArrayList<TimeMachiningVo>();
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from TimeMachiningEntity entity ";
        List<TimeMachiningEntity> list = timeMachiningDao.findList(hql + whereHql(timeMachiningVo, params) + orderHql(pageFilter), params, pageFilter.getPage(), pageFilter.getRows());
        for (TimeMachiningEntity entity : list) {
            TimeMachiningVo u = new TimeMachiningVo();
            BeanUtils.copyProperties(entity, u);

            u.setEquipmentId(entity.getEquipmentEntity().getId());
            u.setMachiningId(entity.getMachiningEntity().getId());
            u.setEquipmentName(entity.getEquipmentEntity().getEquipmentName());
            u.setMachiningName(entity.getMachiningEntity().getMachiningName());
            ul.add(u);
        }
        return ul;
    }

    @Override
    public Long count(TimeMachiningVo timeMachiningVo, PageFilter pageFilter) {
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from TimeMachiningEntity entity ";
        return timeMachiningDao.count("select count(*) " + hql + whereHql(timeMachiningVo, params), params);
    }

    private String whereHql(TimeMachiningVo timeMachiningVo, Map<String, Object> params) {
        String hql = "";
        if (timeMachiningVo != null) {
            hql += " where 1=1 ";
        }
        return hql;
    }

    private String orderHql(PageFilter pageFilter) {
        String orderString = "";
        if ((pageFilter.getSort() != null) && (pageFilter.getOrder() != null)) {
            orderString = " order by entity." + pageFilter.getSort() + " " + pageFilter.getOrder();
        }
        return orderString;
    }

    @Override
    public List<Tree> tree() {
        List<TimeMachiningEntity> l = null;
        List<Tree> lt = new ArrayList<Tree>();

        l = timeMachiningDao.findList("select distinct entity from TimeMachiningEntity entity");

        if ((l != null) && (l.size() > 0)) {
            for (TimeMachiningEntity r : l) {
                Tree tree = new Tree();
                tree.setId(r.getId().toString());
                lt.add(tree);
            }
        }
        return lt;
    }

    @Override
    public List<TimeMachiningEntity> getMachiningByEquip(Integer equipmentId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("equipmentId", equipmentId);
        String hql = "from TimeMachiningEntity c where c.id in (select min(c.id) from TimeMachiningEntity c where c.equipmentEntity.id=:equipmentId group by c.machiningEntity.id)";
        List<TimeMachiningEntity> list = timeMachiningDao.findList(hql, params);
        return list;
    }

    @Override
    public List<TimeMachiningEntity> getDateByEquipMachine(Integer equipmentId, Integer machiningId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("equipmentId", equipmentId);
        params.put("machiningId", machiningId);
        String hql = "from TimeMachiningEntity c where c.id in (select min(c.id) from TimeMachiningEntity c where c.equipmentEntity.id=:equipmentId and c.machiningEntity.id=:machiningId group by c.machiningDate)";
        List<TimeMachiningEntity> list = timeMachiningDao.findList(hql, params);
        return list;
    }

    @Override
    public List<TimeMachiningEntity> getProcessByEquipMachDate(Integer equipmentId, Integer machiningId, Date machiningDate) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("equipmentId", equipmentId);
        params.put("machiningId", machiningId);
        params.put("machiningDate", machiningDate);
        String hql = "from TimeMachiningEntity c where c.equipmentEntity.id=:equipmentId and c.machiningEntity.id=:machiningId and c.machiningDate=:machiningDate";
        List<TimeMachiningEntity> list = timeMachiningDao.findList(hql, params);
        return list;
    }

    @Override
    public List<TimeMachiningEntity> getAllData(Integer equipmentId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("equipmentId", equipmentId);
        String hql = "from TimeMachiningEntity c where c.equipmentEntity.id=:equipmentId";
        List<TimeMachiningEntity> list = timeMachiningDao.findList(hql, params);
        return list;
    }
}
